﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.ZhiJian
{
    public partial class ChanPin_Out_Bill : System.Web.UI.Page
    {
        private string sql =
@"SELECT DISTINCT 
dbo.产品出库单.ID, dbo.产品出库单.序号, 
case
when 产品出库单.生产线 between 100 and  199 then '一分厂'
when 产品出库单.生产线 between 200 and  299 then '二分厂'
when 产品出库单.生产线 =501 then '五分厂1号线'
when 产品出库单.生产线 =502 then '五分厂2号线'
end
as 生产单位
, dbo.产品.品名 + '(' + dbo.产品.钢级 + ')' + dbo.产品.规格 AS 产品类别, 
dbo.产品出库单.总重量, dbo.产品出库单.总长度, dbo.产品出库单.总件数, dbo.产品出库单.日期,dbo.产品出库单.是否合格
FROM  dbo.产品出库单 INNER JOIN
dbo.产品 ON dbo.产品出库单.产品类别ID = dbo.产品.ID INNER JOIN
dbo.产品库存 ON dbo.产品出库单.ID = dbo.产品库存.出库单ID
WHERE  (dbo.产品库存.状态 = 2) AND
(dbo.产品出库单.是否送检=1)";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                sql = sql + " and [产品出库单].[产品类别ID]=" + Request.QueryString["id"];
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        //查询按钮
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string querySql = " ";
            if (Request.QueryString["id"] == "1")
            {
                if (this.FenChang.SelectedValue == "2")
                {
                    querySql = "and 产品出库单.生产线 between 200 and  299";
                }
                if (this.FenChang.SelectedValue == "5")
                {
                    querySql = "and 产品出库单.生产线 between 500 and  599";
                }
            }
            else
            {
                if (this.FenChang1.SelectedValue == "1")
                {
                    querySql = "and 产品出库单.生产线 between 100 and  199";
                }
                if (this.FenChang.SelectedValue == "2")
                {
                    querySql = "and 产品出库单.生产线 between 200 and  299";
                }
            }
            if (this.DanHao.Text.Trim() != "")
            {
                querySql = "and 产品出库单.序号 ='" + this.DanHao.Text.Trim() + "'";
            }

            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                if (Dt.ToString() != "")
                {
                    querySql += "and [日期]>='" + Dt.ToShortDateString() + "'";
                }

            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                if (Dt.ToString() != "")
                {
                    querySql += "and [日期]>='" + Dt.ToShortDateString() + "'";
                }
            }
            //if (querySql == "" && KunHao.Text=="")
            //{
               string sql = @"SELECT DISTINCT 
                      dbo.产品出库单.ID, dbo.产品出库单.序号, case
when 产品出库单.生产线 between 100 and  199 then '一分厂'
when 产品出库单.生产线 between 200 and  299 then '二分厂'
when 产品出库单.生产线 =501 then '五分厂1号线'
when 产品出库单.生产线 =502 then '五分厂2号线'
end
as 生产单位
, dbo.产品.品名 + '(' + dbo.产品.钢级 + ')' + dbo.产品.规格 AS 产品类别, 
                      dbo.产品出库单.总重量, dbo.产品出库单.总长度, dbo.产品出库单.总件数, dbo.产品出库单.日期,dbo.产品出库单.是否合格
                      FROM  dbo.产品出库单 INNER JOIN
                      dbo.产品 ON dbo.产品出库单.产品类别ID = dbo.产品.ID INNER JOIN
                      dbo.产品库存 ON dbo.产品出库单.ID = dbo.产品库存.出库单ID
                      WHERE (dbo.产品出库单.是否送检=1) AND (dbo.产品库存.状态 = 2) and [产品出库单].[产品类别ID]=" + Request.QueryString["id"] + querySql;
            this.SqlDataSource1.SelectCommand = sql;
//            }
//            else
//            {
//                if (KunHao.Text == "")
//                {
//                    this.SqlDataSource1.SelectCommand =
//@"SELECT DISTINCT 
//dbo.产品出库单.ID, dbo.产品出库单.序号, 
//case
//when 产品出库单.生产线 between 100 and  199 then '一分厂'
//when 产品出库单.生产线 between 200 and  299 then '二分厂'
//when 产品出库单.生产线 =501 then '五分厂1号线'
//when 产品出库单.生产线 =502 then '五分厂2号线'
//end
//as 生产单位
//, dbo.产品.品名 + '(' + dbo.产品.钢级 + ')' + dbo.产品.规格 AS 产品类别, 
//dbo.产品出库单.总重量, dbo.产品出库单.总长度, dbo.产品出库单.总件数, dbo.产品出库单.日期,dbo.产品出库单.是否合格
//FROM  dbo.产品出库单 INNER JOIN
//dbo.产品 ON dbo.产品出库单.产品类别ID = dbo.产品.ID INNER JOIN
//dbo.产品库存 ON dbo.产品出库单.ID = dbo.产品库存.出库单ID
//WHERE (dbo.产品出库单.是否送检=1) AND (dbo.产品库存.状态 = 2) and [产品出库单].[产品类别ID]=" + Request.QueryString["id"] + querySql;
//                }
//                else
//                {
//                    this.SqlDataSource1.SelectCommand =
//@"SELECT DISTINCT 
//dbo.产品出库单.ID, dbo.产品出库单.序号, 
//case
//when 产品出库单.生产线 between 100 and  199 then '一分厂'
//when 产品出库单.生产线 between 200 and  299 then '二分厂'
//when 产品出库单.生产线 =501 then '五分厂1号线'
//when 产品出库单.生产线 =502 then '五分厂2号线'
//end
//as 生产单位
//, dbo.产品.品名 + '(' + dbo.产品.钢级 + ')' + dbo.产品.规格 AS 产品类别, 
//dbo.产品出库单.总重量, dbo.产品出库单.总长度, dbo.产品出库单.总件数, dbo.产品出库单.日期,dbo.产品出库单.是否合格
//FROM  dbo.产品出库单 INNER JOIN
//dbo.产品 ON dbo.产品出库单.产品类别ID = dbo.产品.ID INNER JOIN
//dbo.产品库存 ON dbo.产品出库单.ID = dbo.产品库存.出库单ID
//WHERE (dbo.产品出库单.是否送检=1) AND (dbo.产品库存.状态 = 2) and [产品出库单].[产品类别ID]=" + Request.QueryString["id"] + querySql + " and [产品库存].[捆号]='" + this.KunHao.Text + "'";
//                }
                this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
//            }
        }

        //查看
        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                Response.Redirect("ChanPin_Out.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString());
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        //保存结果 
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[12].Controls[1];
                string ID = this.GridView1.Rows[i].Cells[1].Text;
                if (cb.Checked == true)
                {
                    this.SqlDataSource1.UpdateCommand = "Exec RuKuTongZhi_ZhiJian_Update " + "true," + ID;
                    this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
                    this.SqlDataSource1.Update();
                    this.GridView1.DataSourceID = this.SqlDataSource1.ID;
                    this.Message.Text = "·质检信息保存成功";
                }
                else if (cb.Checked == false)
                {
                    this.SqlDataSource1.UpdateCommand = "Exec RuKuTongZhi_ZhiJian_Update " + "false," + ID;
                    this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
                    this.SqlDataSource1.Update();
                    this.GridView1.DataSourceID = this.SqlDataSource1.ID;
                    this.Message.Text = "·质检信息保存成功";
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                Conn.Open();
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Conn;
                Cmd.CommandText = 
@"SELECT DISTINCT 
dbo.产品库存.炉号, dbo.产品理化参数.C, dbo.产品理化参数.Mn, dbo.产品理化参数.Si, dbo.产品理化参数.P, dbo.产品理化参数.S, dbo.产品理化参数.Cr, dbo.产品理化参数.Ni, 
dbo.产品理化参数.Cu, dbo.产品理化参数.Mo, dbo.产品理化参数.Al, dbo.产品理化参数.V, dbo.产品理化参数.Bi, dbo.产品理化参数.Sn, dbo.产品理化参数.Pb, dbo.产品理化参数.[As], 
dbo.产品理化参数.Sb, dbo.产品理化参数.抗拉强度, dbo.产品理化参数.屈服极限, dbo.产品理化参数.伸长率, dbo.产品理化参数.横向冲击, dbo.产品理化参数.纵向冲击, 
dbo.产品理化参数.通径检验, dbo.产品理化参数.静水压试验, dbo.产品理化参数.无损探伤, dbo.产品理化参数.表面检验, dbo.产品理化参数.尺寸
FROM dbo.产品库存 INNER JOIN
dbo.产品理化参数 ON dbo.产品库存.炉号 = dbo.产品理化参数.炉号 where 出库单ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
                SqlDataReader Reader = Cmd.ExecuteReader();
                if (!Reader.Read())
                {
                    this.Message.Text = ".该炉号信息不存在,请设置<a href='产品理化参数.aspx?type=" + Request.QueryString["id"] + "'>产品理化参数</a>对该炉号进行添加！";
                    //string strScript = "<script language=javascript>window.alert(该炉号信息不存在，请添加该炉号信息！)</script>";

                    //Response.Write(strScript);
                    Reader.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                    return;
                }
                Reader.Close();
                Reader.Dispose();
                Cmd.Dispose();
                Conn.Dispose();
                Response.Redirect("产品质保书.aspx?ckid=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString());
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        ////明细
        protected void Button_MX_Click(object sender, EventArgs e)
        {
            string id = ((Button)sender).CommandName;
            Response.Redirect("/ZhiJian/产品出库单.aspx?id=" + id);

        }


        //合格判定
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            int k = 0;
            if (((CheckBox)sender).Checked) k = 1;
            this.SqlDataSource1.UpdateCommand = "update [产品出库单] set [是否合格]=" + k + " where id=" + ((CheckBox)sender).ToolTip;
            this.SqlDataSource1.Update();
            this.GridView1.SelectedIndex = -1;
        }

    }
}
